package com.wl.shop.web;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wl.shop.entity.Article;
import com.wl.shop.entity.Attachment;
import com.wl.shop.model.DataTableResult;
import com.wl.shop.utils.Constants;

@RequestMapping("${adminPath}/attachment")
@Controller
public class AttachmentController extends BaseController{
	
	@GetMapping("list")
	public String list(Model model){
		return "attachment/pictures/list";
	}
	
	@PostMapping("list")
	@ResponseBody
	public DataTableResult list(String aodata,ServletRequest request){
		Map<String,Object>	map =Constants.convertToMap(aodata);
		int echo = (Integer) map.get("sEcho");
		int start = (Integer)map.get("iDisplayStart");
		int pageSize = (Integer)map.get("iDisplayLength");
		pageSize=pageSize<0?1000:pageSize;
		int pageNum = start/pageSize==0?1:(start/pageSize+1);
		DataTableResult result = new DataTableResult();
		result.setSEcho(echo);
		EntityWrapper<Attachment> entity = new EntityWrapper<Attachment>();
		entity.orderBy("createDate",false);
		Page<Attachment> page = attachmentService.selectPage(new Page<Attachment>(pageNum,pageSize), entity);
		result.setAaData(page.getRecords());
		result.setITotalRecords(page.getTotal());
		result.setITotalDisplayRecords(page.getTotal());
		return result;
	}
}
